package com.emperor.service.impl;

import com.emperor.dto.RegionAddDto;
import com.emperor.entity.Area;
import com.emperor.mapper.RegionMapper;
import com.emperor.service.RegionService;
import com.emperor.vo.RegionPageVo;
import com.emperor.vo.SkuClassPageVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public SkuClassPageVo findAll(Integer pageIndex, Integer pageSize, String className) {
        PageHelper.startPage(pageIndex, pageSize);
        List<RegionPageVo> list = regionMapper.findAll(className);
        Page<RegionPageVo> p = (Page<RegionPageVo>) list;
        // 计算总页数
        long l = (p.getTotal() / pageSize) + 1;
        // 创建SkuClassPageVo对象
        SkuClassPageVo build = SkuClassPageVo.builder().
                pageIndex(pageIndex).
                pageSize(pageSize)
                .totalPage((int) l).totalCount((int) p.getTotal())
                .currentPageRecords(new List[]{p.getResult()}).build();
        // 返回SkuClassPageVo对象
        return build;
    }

    @Override
    public void add(RegionAddDto dto) {
        regionMapper.add(dto);
    }

    @Override
    public void updateId(Integer regionId, RegionAddDto dto) {
        regionMapper.updateId(regionId, dto);
    }

    @Override
    public void deleteById(Integer regionId) {
        regionMapper.deleteById(regionId);
    }
}
